<template>
  <div class="app-container">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="学科：" prop="subject" required>
        <el-select v-model="form.subject" placeholder="学科">
          <el-option v-for="item in subjects" :key="item.id" :value="item.id"
                     :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="试卷名称：" prop="name" required>
        <el-input v-model="form.name" class="input-name"/>
      </el-form-item>
      <el-form-item :key="index" :label="'标题'+(index+1)+'：'" required v-for="(topic,index) in form.topics">
        <el-input v-model="topic.content" style="width: 90%"/>
        <el-button type="text" class="link-left" style="margin-left: 20px" size="mini" @click="addQuestion(topic)">
          添加题目
        </el-button>
        <!--        删除大标题-->
        <el-button type="text" class="link-left" size="mini" @click="removeTopic(index)">删除</el-button>
        <el-card class="exampaper-item-box" v-if="topic.titles.length!==0">
          <el-form-item :key="titleIndex" :label="'题目'+(titleIndex+1)+'：'"
                        v-for="(title,titleIndex) in topic.titles" style="margin-bottom: 15px">
            <el-row>
              <el-col :span="23">
                <ShowTitle :qType="title.title_type" :question="title"/>
              </el-col>
              <el-col :span="1">
                <!--                删除小题-->
                <el-button type="text" size="mini" @click="topic.titles.splice(titleIndex,1)">删除
                </el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-card>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="success" @click="addTopic">添加标题</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="questionPage.showDialog" width="70%">
      <el-form :inline="true">
        <el-form-item>
          <el-input
              v-model="questionPage.search"
              placeholder="输入试题题目搜索"
              clearable
              @change="getTitleList">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getTitleList" class="el-icon-search" type="primary"></el-button>
        </el-form-item>
        <el-form-item>
          <template>
            <el-select v-model="questionPage.title_type" clearable placeholder="按题型分类" @change="getTitleList">
              <el-option
                  v-for="item in questionPage.title_type_list"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>

      <el-table
          ref="multipleTable"
          :data="questionPage.tableData"
          tooltip-effect="dark"
          style="width: 100%"
          border
          stripe
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="40">
        </el-table-column>
        <el-table-column
            prop="id"
            label="id"
            width="70">
        </el-table-column>
        <el-table-column
            prop="theme"
            label="题干"
            width="662">
          <template slot-scope="scope">
            <span v-html="scope.row.theme"></span>
          </template>
        </el-table-column>
        <el-table-column
            prop="subject_name"
            label="学科"
            width="90">
        </el-table-column>
        <el-table-column
            prop="type_name"
            label="题型"
            width="80">
        </el-table-column>
        <el-table-column
            prop="title_score"
            label="分数"
            width="80">
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 50, 100]"
          :current-page="questionPage.current"
          :page-size="questionPage.size"
          :total="questionPage.total">
      </el-pagination>
      <div class="select-btn">
        <el-button @click="questionPage.showDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmQuestionSelect">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import ShowTitle from "../../components/ShowTitle";

export default {
  components: {ShowTitle},
  data() {
    return {
      form: {
        id: null,
        subject: null,
        name: '',
        topics: []
      },
      subjects: null,
      formLoading: false,
      rules: {
        subject: [
          {required: true, message: '请选择学科', trigger: 'change'}
        ],
        name: [
          {required: true, message: '请输入试卷名称', trigger: 'blur'}
        ],
      },
      questionPage: {
        listLoading: true,
        showDialog: false,
        title_type: null,
        title_type_list: [
          {id: 0, name: '单选题'},
          {id: 1, name: '多选题'},
          {id: 2, name: '判断题'},
          {id: 3, name: '简答题'},
        ],
        search: '',
        total: 0,
        size: 5,
        current: 1,
        tableData: [],
        multipleSelection: [],
      },
      currentTitleItem: null
    }
  },
  created() {
    let id = this.$route.query.id
    this.getSubjects()
    if (id) {
      this.getPaper(id)
    }
  },
  methods: {
    getPaper(id) {
      this.$http({
        url: this.$settings.BASE_URL + `/paper/paper_List/${id}/`,
        method: 'get',
      }).then(res => {
        if (res.data.code === 20000) {
          this.form = res.data.result
          this.form.topics = res.data.result.topicItem
        } else {
          this.$message({
            type: 'error',
            message: '系统错误,请稍后重试',
            duration: 3000,
          })
        }
      })
    },
    getSubjects() {
      this.$http({
        url: this.$settings.BASE_URL + '/paper/subject_list/',
        method: 'get',
      }).then(res => {
        if (res.data.code === 20000) {
          this.subjects = res.data.result.results
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    handleSizeChange(val) {
      this.questionPage.size = val
      this.getTitleList()
    },
    handleCurrentChange(val) {
      this.questionPage.current = val
      this.getTitleList()
    },
    submitForm() {
      this.$http({
        url: this.$settings.BASE_URL + '/paper/paper_List/create_paper/',
        method: 'post',
        data: this.form,
      }).then(res => {
        if (res.data.code === 20000) {
          this.$message({
            type: 'success',
            message: res.data.msg,
            duration: 3000,
          })
          this.resetForm()
        } else {
          this.$message({
            type: 'error',
            message: '提交失败,请检查是否填写完整',
            duration: 3000,
          })
        }
      })
    },
    addTopic() {
      this.form.topics.push({
        content: '',
        titles: []
      })
      this.getSubjects()
    },
    removeTopic(index) {
      this.form.topics.splice(index, 1)
      this.getSubjects()
    },
    addQuestion(topic) {
      this.currentTitleItem = topic
      this.questionPage.showDialog = true
      this.getTitleList()
    },
    confirmQuestionSelect() {
      this.questionPage.multipleSelection.forEach(title => {
        this.currentTitleItem.titles.push(title)
      })
      this.questionPage.showDialog = false
    },
    getTitleList() {
      this.questionPage.listLoading = true
      this.$http({
        url: this.$settings.BASE_URL + '/paper/title_list/',
        method: 'get',
        params: {
          page: this.questionPage.current,
          size: this.questionPage.size,
          search: this.questionPage.search,
          title_type: this.questionPage.title_type,
          subject: this.form.subject,
        },
      }).then(res => {
        if (res.data.code === 20000) {
          this.questionPage.tableData = res.data.result.results
          this.questionPage.total = res.data.result.count
          this.questionPage.listLoading = false
        } else {
          this.$message({
            type: 'warning',
            message: res.data.msg,
            duration: 3000,
          })
        }
      })
    },
    handleSelectionChange(val) {
      this.questionPage.multipleSelection = val
    },
    resetForm() {
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        subject: null,
        name: '',
        topics: []
      }
    },
  },
  computed: {}
}
</script>

<style scoped>
.input-name {
  width: 650px;
}

.select-btn {
  position: relative;
  left: 426px;
  width: 200px;
}
</style>
